package com.open.capacity.prints.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.prints.dao.BillDgjsywsqDao;
import com.open.capacity.prints.entity.BillDgjsywsq;
import com.open.capacity.prints.service.BillDgjsywsqService;
import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class BillDgjsywsqServiceImpl implements BillDgjsywsqService {

    @Autowired
    private BillDgjsywsqDao billDgjsywsqDao;

    /**
     * 添加
     *
     * @param billDgjsywsq
     */
    public String save(BillDgjsywsq billDgjsywsq) {
        billDgjsywsq.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billDgjsywsq.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        if(billDgjsywsq.getDjrq() != ""){
            calendar.setTime(DateUtil.parseDate2(billDgjsywsq.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
            billDgjsywsq.setYear(calendar.get(Calendar.YEAR));                    //获取年份
            billDgjsywsq.setMonth(String.valueOf(calendar.get(Calendar.MONTH)+1));                    //获取月份
            billDgjsywsq.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
            billDgjsywsq.setDjrq(billDgjsywsq.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
        if(billDgjsywsq.getMoney2() != null) {
            billDgjsywsq.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billDgjsywsq.getMoney2().divide(BigDecimal.valueOf(100))));
            billDgjsywsq.setMoney2(billDgjsywsq.getMoney2().divide(BigDecimal.valueOf(100)));
        }
        billDgjsywsqDao.save(billDgjsywsq);
        return billDgjsywsq.getId();
    }

    /**
     * 修改
     *
     * @param billDgjsywsq
     */
    public String update(BillDgjsywsq billDgjsywsq) {
        billDgjsywsq.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();
        if(billDgjsywsq.getDjrq() != ""){
            calendar.setTime(DateUtil.parseDate2(billDgjsywsq.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
            billDgjsywsq.setYear(calendar.get(Calendar.YEAR));                    //获取年份
            billDgjsywsq.setMonth(String.valueOf(calendar.get(Calendar.MONTH)+1));                    //获取月份
            billDgjsywsq.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
            billDgjsywsq.setDjrq(billDgjsywsq.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
        if(billDgjsywsq.getMoney2() != null) {
            billDgjsywsq.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billDgjsywsq.getMoney2().divide(BigDecimal.valueOf(100))));
            billDgjsywsq.setMoney2(billDgjsywsq.getMoney2().divide(BigDecimal.valueOf(100)));
        }
        billDgjsywsqDao.update(billDgjsywsq);
        return billDgjsywsq.getId();
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billDgjsywsqDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillDgjsywsq findById(String id) {
        BillDgjsywsq item = billDgjsywsqDao.findById(id);
        BigDecimal resut = item.getMoney2();
        if(resut != null) {
            Long million7Count = resut.divide(BigDecimal.valueOf(10000000000L)).longValue();
            item.setMillion7(million7Count);
            resut = resut.subtract(BigDecimal.valueOf(million7Count * 10000000000L));

            Integer million6Count = resut.divide(BigDecimal.valueOf(1000000000)).intValue();
            item.setMillion6(million6Count);
            resut = resut.subtract(BigDecimal.valueOf(million6Count * 1000000000));

            Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
            item.setMillion5(million5Count);
            resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

            Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
            item.setMillion4(million4Count);
            resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


            Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion3(million3Count);
            resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

            Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion2(million2Count);
            resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

            Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion1(million1Count);
            resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

            Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand(thousandCount);
            resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

            Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred(hundredCount);
            resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

            Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen(tenCount);
            resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

            Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan(yuanCount);
            resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

            Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao(jiaoCount);
            resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

            Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
            item.setFen(fenCount);
        }

        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillDgjsywsq> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillDgjsywsq> list = billDgjsywsqDao.findAll(params);
        list.forEach(item -> {
            BigDecimal resut = item.getMoney2();
            if(resut != null) {
                Long million7Count = resut.divide(BigDecimal.valueOf(10000000000L)).longValue();
                item.setMillion7(million7Count);
                resut = resut.subtract(BigDecimal.valueOf(million7Count * 10000000000L));

                Integer million6Count = resut.divide(BigDecimal.valueOf(1000000000)).intValue();
                item.setMillion6(million6Count);
                resut = resut.subtract(BigDecimal.valueOf(million6Count * 1000000000));

                Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
                item.setMillion5(million5Count);
                resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

                Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
                item.setMillion4(million4Count);
                resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


                Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
                item.setMillion3(million3Count);
                resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

                Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
                item.setMillion2(resut.divide(BigDecimal.valueOf(100000)).intValue());
                resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

                Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
                item.setMillion1(million1Count);
                resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

                Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
                item.setThousand(thousandCount);
                resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

                Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
                item.setHundred(hundredCount);
                resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

                Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
                item.setTen(tenCount);
                resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

                Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
                item.setYuan(yuanCount);
                resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

                Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
                item.setJiao(jiaoCount);
                resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

                Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).intValue();
                item.setFen(fenCount);
            }
        });
        PageInfo<BillDgjsywsq> pageInfo = new PageInfo(list);

        return PageResult.<BillDgjsywsq>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillDgjsywsq> findAll(Map<String, Object> params) {
        return billDgjsywsqDao.findAll(params);
    }

    /**
     * 查询记录
     *
     * @param sqrFullname
     * @return
     */
    public List<BillDgjsywsq> findBySqrFullname(String sqrFullname) {
        return billDgjsywsqDao.findBySqrFullname(sqrFullname);
    }

    /**
     * 查询记录
     *
     * @param skrFullname
     * @return
     */
    public List<BillDgjsywsq> findBySkrFullname(String skrFullname) {
        return billDgjsywsqDao.findBySkrFullname(skrFullname);
    }
}
